import importlib
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from reportservice.helpers import ApiResponse
from yida_app_service.xu_digital_sign_client import XmDigitalSignBaseClient

def get_xm_digital_sign_client(company_name):
    """
    根据提供的模块名称动态导入XmDigitalSignClient类。

    :param company_name: 模块名称，比如 'zhongcheng' 或 'shujian'
    :return: 导入的XmDigitalSignClient类
    """
    module_name = f'yida_app_service.{company_name}.xm_digital_sign_config'
    module = importlib.import_module(module_name)
    return getattr(module, 'XmDigitalSignClient')
@csrf_exempt
@require_http_methods(["GET"])
def view_signed_documnent(request,company_name):
    order_no = request.GET.get("orderNo")
    XmDigitalSignClient = get_xm_digital_sign_client(company_name)
    xm_digital_sign_client:XmDigitalSignBaseClient = XmDigitalSignClient()
    result = xm_digital_sign_client.view_signed_file(order_no)
    if(result.get("code")==200):
        return redirect(result.get("data"))
    else:
        return ApiResponse.fail("未能查到签名文件，请联系管理员")